<template>
	<div class="page-info">
		<div class="search">
			<div class="search-box">
				<a-form
					:model="searchInfo"
					:label-col="labelCol"
					:wrapper-col="wrapperCol"
				>
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item
								:name="labelMap['name'].name"
								:label="labelMap['name'].label"
							>
								<a-input
									style="width: 90%; margin-left: 8px"
									v-model:value="searchInfo.name"
									:placeholder="'请输入' + labelMap['name'].label"
									allow-clear
								/>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item
								:name="labelMap['keyword'].name"
								:label="labelMap['keyword'].label"
							>
								<a-input
									style="width: 90%; margin-left: 8px"
									v-model:value="searchInfo.keyword"
									:placeholder="'请输入' + labelMap['keyword'].label"
									allow-clear
								/>
							</a-form-item>
						</a-col>
						<a-col style="text-align: right; margin-bottom: 20px">
							<a-space>
								<a-button type="primary" @click="query"> 查找</a-button>
								<a-button type="primary" @click="cancelQuery">清空</a-button>
							</a-space>
						</a-col>
					</a-row>
				</a-form>
			</div>
		</div>
		<div class="button" style="margin: 10px">
			<a-space>
				<a-button type="primary" @click="editOfflineServiceProvider('add')"
					>新增</a-button
				>
				<a-button type="primary" danger @click="batchDelOfflineServiceProvider"
					>删除</a-button
				>
			</a-space>
		</div>
		<div class="content">
			<a-table
				:dataSource="dataSource"
				:columns="columns"
				:loading="loading"
				:row-key="(record) => record.id"
				:pagination="pagination"
				@change="handleTableChange"
				:scroll="{ y: 470, x: 'max-content' }"
				:row-selection="rowSelection"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.key === 'operation'">
						<a-space>
							<!-- <a-popconfirm title="确认审批?" ok-text="确认" cancel-text="取消"
                @confirm="approveOfflineServiceProviderInfo(record.id)" @cancel="cancel"
                v-if="record.type === 'partner' && record.status !== 2">
                <a-button type="primary" size="small">审核</a-button>
              </a-popconfirm> -->
							<a-button
								type="primary"
								size="small"
								style="background-color: green"
								@click="editOfflineServiceProvider('update', record.id)"
								>编辑</a-button
							>
							<a-popconfirm
								title="确认删除?"
								ok-text="确认"
								cancel-text="取消"
								@confirm="delOfflineServiceProvider(record.id)"
								@cancel="cancel"
							>
								<a-button type="primary" size="small" danger>删除</a-button>
							</a-popconfirm>
						</a-space>
						<span></span>
					</template>
					<template v-else-if="column.key === 'provinceName'">
						{{
							(record.provinceName ?? '') +
							' ' +
							(record.cityName ?? '') +
							' ' +
							(record.districtName ?? '')
						}}
					</template>
					<template v-else-if="column.key === 'singleFlag'">
						<a-tag
							v-if="record.type === 'partner'"
							:key="record.singleFlag"
							:color="record.singleFlag == 1 ? '#87d068' : 'grey'"
						>
							{{ record.singleFlag == 1 ? '是' : '否' }}
						</a-tag>
					</template>
					<template v-else-if="column.key === 'status'">
						<a-tag
							v-if="record.type === 'partner'"
							:key="record.statusFlag"
							:color="record.status == 2 ? '#87d068' : 'grey'"
						>
							{{ record.status == 2 ? '审核通过' : '申请中' }}
						</a-tag></template
					>
				</template>
			</a-table>
			<UserPartnerInfoDetail
				ref="editInfo"
				:open="visible"
				:modelInfo="modelInfo"
				@handleOk="handleOk"
				@handleCancel="handleCancel"
			></UserPartnerInfoDetail>
		</div>
	</div>
</template>
<script setup lang="ts">
import {
	SearchInfo,
	pagination,
	columns,
	DataItem,
	ModelInfo,
	pageInfo,
} from './userPartnerListTs';
import {
	getOfflineServiceProviderPage,
	deleteOfflineServiceProvider,
} from '@/api/userInfo/offlineServiceProvider/offlineServiceProviderTs';
import { message } from 'ant-design-vue';

const labelCol = ref({ span: 5 });
const wrapperCol = ref({ span: 19 });

let rowIds = [] as any;

let visible = ref<boolean>(false);

let modelInfo = ref<ModelInfo>({});

let loading = ref<boolean>(false);

let dataSource = ref();

const rowSelection = ref({
	checkStrictly: false,
	onChange: (
		selectedRowKeys: (string | number)[],
		selectedRows: DataItem[],
	) => {
		rowIds = selectedRowKeys;
	},
	onSelect: (record: DataItem, selected: boolean, selectedRows: DataItem[]) => {
		console.log(record, selected, selectedRows);
	},
	onSelectAll: (
		selected: boolean,
		selectedRows: DataItem[],
		changeRows: DataItem[],
	) => {
		console.log(selected, selectedRows, changeRows);
	},
});

const labelMap = ref<any>({
	userId: { name: 'userId', label: '用户ID，关联用户表' },
	contactInfo: { name: 'contactInfo', label: '联系方式' },
	name: { name: 'name', label: '姓名' },
	phoneNumber: { name: 'phoneNumber', label: '手机号' },
	demandDescription: { name: 'demandDescription', label: '需求描述' },
	keyword: { name: 'keyword', label: '编码/电话' },
});

let searchInfo = ref<SearchInfo>({});

const cancelQuery = (): void => {
	searchInfo.value = {};
};

const query = (): void => {
	getOfflineServiceProviderListPage(searchInfo.value, pagination.value);
};

const handleTableChange = (pagination): void => {
	getOfflineServiceProviderListPage(searchInfo.value, pagination);
};

const delOfflineServiceProvider = (ids: string): void => {
	deleteOfflineServiceProvider(ids).then((res) => {
		if (res.code == '200') {
			message.success((res && '删除' + res.message) || '删除成功！', 3);
			getOfflineServiceProviderListPage(searchInfo.value, pagination.value);
		} else {
			message.error((res && res.message) || '删除失败！', 3);
		}
	});
};

const batchDelOfflineServiceProvider = (): void => {
	let ids = '';
	if (rowIds && rowIds.length > 0) {
		rowIds.forEach((item: string) => {
			ids += item + ',';
		});
		ids = ids.substring(0, ids.length - 1);
	} else {
		message.warning('请先选择数据！', 3);
		return;
	}
	delOfflineServiceProvider(ids);
};

// 取消
const cancel = (e: MouseEvent): void => {
	console.log(e);
};

// 获取合伙人列表
const getOfflineServiceProviderListPage = (
	param: SearchInfo,
	cur: pageInfo,
): void => {
	loading.value = true;
	param.type = 'partner';
	getOfflineServiceProviderPage(param, cur.current, cur.pageSize)
		.then((res) => {
			if (res.code == '200') {
				dataSource.value = res.data.records;
				pagination.value.current = res.data.current;
				pagination.value.pageSize = res.data.size;
				pagination.value.total = res.data.total;
			} else {
				message.error((res && res.message) || '查询列表失败！');
			}
		})
		.finally(() => {
			loading.value = false;
		});
};

//新增和修改弹窗
const editOfflineServiceProvider = (type: string, id?: number): void => {
	if (type == 'add') {
		modelInfo.value.title = '新增明细';
		modelInfo.value.id = undefined;
	} else if (type == 'update') {
		modelInfo.value.title = '修改明细';
		modelInfo.value.id = id;
	}
	modelInfo.value.confirmLoading = true;
	visible.value = true;
};

const handleOk = (v: boolean): void => {
	visible.value = v;
	getOfflineServiceProviderListPage(searchInfo.value, pagination.value);
};

const handleCancel = (v: boolean): void => {
	visible.value = v;
};

// 初始化
const init = (): void => {
	console.log(`init ttttttttttttttttttttttttttttttttt`);
	//获取线下服务商表页面数据
	getOfflineServiceProviderListPage(searchInfo.value, pagination.value);
};

init();
</script>
<style lang="scss" scoped></style>
